namespace Asmex.FileViewer
{
    using System;
    using System.IO;

    public class ImportAddress
    {
        private bool _ByOrdinal;
        private string _Name;
        private uint _Ordinal;

        public ImportAddress(uint n, BinaryReader reader, MModule mod)
        {
            if ((n & 0x80000000) != 0)
            {
                this._ByOrdinal = true;
            }
            else
            {
                this._ByOrdinal = false;
            }
            if (this._ByOrdinal)
            {
                this._Ordinal = n & 0x7fffffff;
            }
            else
            {
                uint rva = n & 0x7fffffff;
                long position = reader.BaseStream.Position;
                reader.BaseStream.Position = mod.ModHeaders.Rva2Offset(rva);
                reader.ReadInt16();
                for (byte i = reader.ReadByte(); i != 0; i = reader.ReadByte())
                {
                    this._Name = this._Name + ((char) i);
                }
                reader.BaseStream.Position = position;
            }
        }

        public override string ToString()
        {
            if (this._ByOrdinal)
            {
                return ("By Ordinal: " + this._Ordinal);
            }
            return ("By Name: " + this._Name);
        }

        public bool ByOrdinal
        {
            get
            {
                return this._ByOrdinal;
            }
            set
            {
                this._ByOrdinal = value;
            }
        }

        public string Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        public uint Ordinal
        {
            get
            {
                return this._Ordinal;
            }
            set
            {
                this._Ordinal = value;
            }
        }
    }
}
